import {
	sub,
	pub
} from '@/common/event.js'
import store from '../index.js'
import moment from 'moment'
import config from '../../config.js'
import {
	getRqst
} from '../../common/request'
export default {
	namespaced: true,
	state: {
		list: [],
		chatgroup: [],
		groups: {},
		current: {}
	},
	mutations: {
		cleardata: (state, payload) => {
			state.list = []
			state.chatgroup = []
			state.groups = {}
			state.current = {}
		},
		relinkgetdata: (state, payload) => {
			
		},
		changegrelation: (state, payload) => {
			for (let o of state.list) {
				if (o.gid > 0 && o.gid == payload.id) {
					o.status = payload.status
					break
				}
			}

			for (let o of state.chatgroup) {
				if (o.id == payload.id) {
					o.status = payload.status
					break
				}
			}
			
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		deletegroup: (state, payload) => {
			const id = payload.id
			delete state.groups[id]
			state.chatgroup = state.chatgroup.filter(e => e.id != id)
			const user = uni.getStorageSync("user")
			state.list = state.list.filter(e => e.gid != id)
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		exitgroup: (state, payload) => {
			const id = payload.id
			delete state.groups[id]
			const user = uni.getStorageSync("user")
			state.list = state.list.filter(e => e.gid != id)
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		letdelgroup: (state, payload) => {
			const id = payload.id
			const name = payload.name
			delete state.groups[id]
			state.list = state.list.filter(e => e.gid != id)
			state.chatgroup = state.chatgroup.filter(e => e.id != id)
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		groupuserexit: (state, payload) => {
			const {
				user,
				chatgroup
			} = payload
			if (state.groups[chatgroup.id]) {
				state.groups[chatgroup.id] = state.groups[chatgroup.id].filter(e => e.uid != user.id)
			}
			for (let o of state.list) {
				if (o.gid == chatgroup.id) {
					if (o.users) {
						o.users = o.users.filter(e => e.uid != user.id)
					}
					break
				}
			}
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		setGroupUsers: (state, payload) => {
			state.groups[payload.id] = payload.users
			for (let o of state.list) {
				if (o.gid == payload.id) {
					o.users = payload.users
					break
				}
			}
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		setLastInfo: (state, payload) => {
			for (let o of state.list) {
				if (payload.gid > 0) {
					if (o.gid == payload.gid) {
						o.message = payload.content
						o.messagetype = payload.type
						o.lasttime = moment().valueOf()
					}
				} else {
					if (o.fid == payload.toid) {
						o.message = payload.content
						o.messagetype = payload.type
						o.lasttime = moment().valueOf()
					}
				}
			}
		},
		setLastInfox: (state, payload) => {
			for (let o of state.list) {
				if (payload.gid > 0) {
					if (o.gid == payload.gid) {
						o.message = payload.content
						o.messagetype = payload.type
						o.lasttime = moment().valueOf()
						o.count++
					}
				} else {
					if (o.fid == payload.uid) {
						o.message = payload.content
						o.messagetype = payload.type
						o.lasttime = moment().valueOf()
						o.count++
					}
				}
			}
		},
		setUserData: (state, payload) => {
			for(let o of state.list){
				if(o.gid == 0 && o.fid == payload.id){
					o.status = payload.status
					o.name = payload.name
					break
				}
			}
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		addNewRelationData: (state, payload) => {
			payload.count = 1
			state.list.unshift(payload)
		},
		addNewChatgroup: (state, payload) => {
			state.chatgroup.push(payload.data)
		},
		setChatGroupInfo: (state, payload) => {
			for (let o of state.list) {
				if (o.gid == payload.id) {
					o.name = payload.name
					o.status = payload.status
					break
				}
			}

			for (let o of state.chatgroup) {
				if (o.id == payload.id) {
					o.name = payload.name
					for (let k in payload) {
						o[k] = payload[k]
					}
					break
				}
			}

			state.list = JSON.parse(JSON.stringify(state.list))
		},
		setgroupuser: (state, payload) => {
			state.groups[payload.id] = payload.users
			for (let o of state.list) {
				if (o.gid == payload.id) {
					o.users = JSON.parse(JSON.stringify(payload.users))
					break
				}
			}
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		setGroupInfo: (state, payload) => {
			for (let o of state.list) {
				if (o.gid == payload.gid) {
					o.message = payload.content
					o.messagetype = payload.type
					o.lasttime = moment().valueOf()
					o.users = payload.leftusers
					break
				}
			}
			if (state.groups[payload.gid]) {
				state.groups[payload.gid] = payload.leftusers
			}
			state.list = JSON.parse(JSON.stringify(state.list))
		},
		removeGroupInfo: (state, payload) => {
			state.list = state.list.filter(e => e.gid != payload.gid)
		},
		setCurrent: (state, payload) => {
			state.current = payload
			for (let o of state.list) {
				if (o.id == payload.id) {
					o.count = 0
					break
				}
			}
			let count = 0
			for (let o of state.list) {
				count = count + o.count * 1
			}

			store.commit(`tabbar/setCount`, {
				key: 0,
				data: count
			})
		},
		setList: (state, payload) => {
			state.list = payload
			for (let o of state.list) {
				if (o.gid > 0 && state.groups[o.gid]) {
					o.users = state.groups[o.gid]
					o.messages = []
				} else {
					o.messages = []
				}
			}
			pub("friendrelation_done", payload)
		},
		setDataItem: (state, payload) => {
			for (let o of state.list) {
				if (o.id == payload.id) {
					for (let k in payload.data) {
						o[k] = payload.data[k]
					}
					break
				}
			}
		},
		setLasttime: (state, payload) => {
			const nt = moment().valueOf()
			for (let o of state.list) {
				if (payload.gid > 0) {
					if (o.gid == payload.gid) {
						if (state.current.gid != o.gid) {
							o.count = o.count + 1
						}
						o.lasttime = nt
						o.message = payload.content
						o.messagetype = payload.type
						getRqst().put(`bgyx/bgyx/putdata/friendrelation`, {
							id: o.id,
							lasttime: nt
						})
						break
					}
				} else {
					if (o.fid == payload.uid) {
						if (state.current.fid != o.fid) {
							o.count = o.count + 1
						}
						o.lasttime = nt
						o.message = payload.content
						o.messagetype = payload.type
						getRqst().put(`bgyx/bgyx/putdata/friendrelation`, {
							id: o.id,
							lasttime: nt
						})
						break
					}
				}

			}

			let count = 0
			for (let o of state.list) {
				count = count + o.count * 1
			}

			store.commit(`tabbar/setCount`, {
				key: 0,
				data: count
			})
		},
		setLasttimex: (state, payload) => {
			const nt = moment().valueOf()
			for (let o of state.list) {
				if (payload.gid > 0) {
					if (o.gid == payload.gid) {
						if (state.current.gid != o.gid) {
							o.count = o.count + 1
						}
						o.lasttime = nt
						o.message = payload.content
						o.messagetype = payload.type
						getRqst().put(`bgyx/bgyx/putdata/friendrelation`, {
							id: o.id,
							lasttime: nt
						})
						break
					}
				} else {
					if (o.fid == payload.toid) {
						if (state.current.fid != o.fid) {
							o.count = o.count + 1
						}
						o.lasttime = nt
						o.message = payload.content
						o.messagetype = payload.type
						getRqst().put(`bgyx/bgyx/putdata/friendrelation`, {
							id: o.id,
							lasttime: nt
						})
						break
					}
				}

			}

			let count = 0
			for (let o of state.list) {
				count = count + o.count * 1
			}

			store.commit(`tabbar/setCount`, {
				key: 0,
				data: count
			})
		},
		setTime: (state, payload) => {
			for (let o of state.list) {
				if (o.id == payload.id) {
					o.time = payload.time
					break
				}
			}
			let lfts = 0
			for (let o of state.list) {
				lfts = lfts + o.count * 1
			}
			store.commit(`tabbar/setCount`, {
				key: 0,
				data: lfts
			})
		},
		setFCount: (state, payload) => {
			for (let o of state.list) {
				if (o.fid == payload.toid) {
					if (payload.count) {
						o.count = payload.count
					} else {
						o.count = o.count + 1
					}
					break
				}
			}
		},
		setGCount: (state, payload) => {
			for (let o of state.list) {
				if (o.gid == payload.gid) {
					if (payload.count) {
						o.count = payload.count
					} else {
						o.count = o.count + 1
					}
					break
				}
			}
		},
		updateLastInfo: (state, payload) => {
			for (let o of state.list) {
				if (payload.gid > 0) {
					if (o.gid == payload.gid) {
						o.message = payload.content
						break
					}
				} else {
					if (o.fid == payload.toid) {
						o.message = payload.content
						break
					}
				}
			}
		},
		remhmd: (state, payload) => {
			for (let o of state.list) {
				if (o.fid == payload.fid) {
					o.status = "正常"
					break
				}
			}
		},
		remhmdb: (state, payload) => {
			for (let o of state.list) {
				if (o.fid == payload.fid && o.status == '被拉黑') {
					o.status = "正常"
					break
				}
			}
		},
		laheiuser: (state, payload) => {
			for (let o of state.list) {
				if (o.fid == payload.fid) {
					o.status = "拉黑"
					break
				}
			}
		},
		laheiuserb: (state, payload) => {
			for (let o of state.list) {
				if (o.fid == payload.fid) {
					o.status = "被拉黑"
					break
				}
			}
		},
		removemessage: (state, payload) => {
			if (payload.gid == 0) {
				for (let o of state.list) {
					if (o.fid == payload.toid || o.fid == payload.uid) {
						o.messages = o.messages.filter(e => e.id != payload.id)
						o.message = "消息已撤回.."
						if (o.count > 0) {
							o.count = o.count - 1
						}
					}
				}
			} else {
				for (let o of state.list) {
					if (o.gid == payload.gid) {
						o.messages = o.messages.filter(e => e.id != payload.id)
						o.message = "消息已撤回.."
						if (o.count > 0) {
							o.count = o.count - 1
						}
						break
					}
				}
			}
			state.list = JSON.parse(JSON.stringify(state.list))
			const lc = store.state.tabbar.list[0].count
			if (lc > 0) {
				store.commit(`tabbar/setCount`, {
					key: 0,
					data: lc - 1
				})
			}

		},
		deluser: (state, payload) => {
			const newlist = []
			for (let o of state.list) {
				if (o.fid != payload.fid) {
					newlist.push(o)
				}
			}
		},
		setGroupUser: (state, payload) => {
			for (let o of state.list) {
				if (o.gid == payload.key) {
					o.users = payload.data
					break
				}
			}
		},
		setUserAllGroups: (state, payload) => {
			state.chatgroup = payload
		},
		setGroups: (state, payload) => {
			state.groups = payload
			for (let k in payload) {
				store.commit(`friend/setGroupUser`, {
					key: k,
					data: payload[k]
				})
			}
		},
		addData: (state, payload) => {
			state.list.unshift(payload)
		},
		setRelationData: (state, payload) => {
			for (let o of state.list) {
				if (o.id == payload.id) {
					for (let k in payload) {
						o[k] = payload[k]
						break
					}
				}
			}
		},
	},
	actions: {
		async init({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`friend/friend/getuser/friendrelation`)
			commit(`setList`, ret.data)
		},
		async addfriendbyid({
			commit,
			state
		}, {
			payload
		}) {
			const user = uni.getStorageSync("user")
			const ret = await getRqst().post(`bgyx/bgyx/getdata/friendrelation`,{
				where:{
					uid:user.id,
					fid:payload
				}
			})
			
			if(ret && ret.success && ret.data && ret.data.length > 0){
				const relat = ret.data[0]
				commit(`addData`, relat)
			}
		},
		async setTimeDb({
			commit,
			state
		}, {
			payload
		}) {
			getRqst().put(`bgyx/bgyx/putdata/friendrelation`, payload)
			commit(`setTime`, payload)
		},
		async loadmorefmessage({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`chat/chat/getuser/fmessage/${payload.fid}`, {
				d: payload.id,
				t: payload.time
			})
			if (ret && ret.success) {
				commit(`addMorefMessage`, {
					data: ret.data,
					item: payload
				})
			}
			return ret
		},
		async initGroups({
			commit,
			state
		}, {
			payload
		}) {
			const user = uni.getStorageSync("user")
			const ret = await getRqst().get(`friend/friend/getgroups/${user.id}`)
			const gidobj = {}
			for (let o of ret.data) {
				if (gidobj[o.gid]) {
					gidobj[o.gid].push(o)
				} else {
					gidobj[o.gid] = [o]
				}
			}
			commit(`setGroups`, gidobj)
		},
		async setnote({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().put(`bgyx/bgyx/putdata/friendrelation`, {
				...payload
			})
			commit(`setRelationData`, payload)
		},
		async handleagree({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().post(`bgyx/bgyx/getdata/friendrelation`, {
				where: payload
			})
			commit(`addData`, ret.data)
		},
		async getAllGroups({
			commit,
			state
		}, {
			payload
		}) {
			const user = uni.getStorageSync("user")
			const ret = await getRqst().get(`friend/friend/getusergroups/${user.id}`)

			commit(`setUserAllGroups`, ret.data)
		},
		async handleagree({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().post(`bgyx/bgyx/getdata/friendrelation`, {
				where: payload
			})
			commit(`addData`, ret.data)
		},
		async addnewgroup({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`friend/friend/getfriendrelation/${payload.id}`)
			if (ret && ret.success) {
				commit(`addNewRelationData`, ret.data)
			}
			const chatgroup = await getRqst().get(`bgyx/bgyx/getdata/chatgroup/${payload.id}`)
			if (chatgroup && chatgroup.success) {
				commit(`addNewChatgroup`, {
					id: payload.id,
					data: chatgroup.data
				})
			}
			const usersret = await getRqst().post(`bgyx/bgyx/getdata/groupuser/`, {
				where: {
					gid: payload.id
				}
			})

			if (usersret && usersret.success) {
				commit(`setGroupUsers`, {
					id: payload.id,
					users: usersret.data
				})
			}
		},
	},
	init: async function() {
		console.log("friend初始化")
		const user = uni.getStorageSync("user")
		if (user && user.id) {
			store.dispatch({
				type: "friend/init",
				payload: {}
			})

			store.dispatch({
				type: "friend/initGroups",
				payload: {}
			})

			store.dispatch({
				type: "friend/getAllGroups",
				payload: {}
			})
		}

		sub("loginSuccess", () => {
			store.dispatch({
				type: "friend/init",
				payload: {}
			})

			store.dispatch({
				type: "friend/initGroups",
				payload: {}
			})

			store.dispatch({
				type: "friend/getAllGroups",
				payload: {}
			})
		})

		sub("logout", function(e) {
			store.commit(`friend/cleardata`, {})
		})

		sub("socket_groupchange", (e) => {
			store.commit("friend/setGroupInfo", e)
		})

		sub("socket_togroup_chatgroupchange", (e) => {
			store.commit("friend/setChatGroupInfo", e)
		})



		sub("socket_getremoveuser", (e) => {
			store.commit("friend/removeGroupInfo", e)
		})


		sub("socket_touser_passapply", (e) => {
			const data = e.item
			store.dispatch({
				type: "friend/init",
				payload: {}
			})

		})

		sub("socket_newgroup", (e) => {
			store.dispatch({
				type: "friend/init",
				payload: {}
			})

			store.dispatch({
				type: "friend/initGroups",
				payload: {}
			})

			store.dispatch({
				type: "friend/getAllGroups",
				payload: {}
			})
		})

		sub("socket_laheiuser", (e) => {
			store.commit("friend/laheiuserb", {
				fid: e.uid
			})

		})

		sub("socket_remhmd", (e) => {
			store.commit("friend/remhmdb", {
				fid: e.uid
			})

		})



		sub("socket_deluser", (e) => {
			store.commit("friend/deluser", {
				fid: e.uid
			})

		})

		sub("socket_touser_message", (e) => {
			store.commit(`friend/setLasttime`, e)
		})

		sub("socket_togroup_message", (e) => {
			store.commit(`friend/setLasttime`, e)
		})

		sub("socket_myself_message", (e) => {
			store.commit(`friend/setLasttimex`, e)
		})

		sub("socket_touser_removemessage", (e) => {
			store.commit(`friend/removemessage`, e)
		})

		sub("socket_togroup_removemessage", (e) => {
			store.commit(`friend/removemessage`, e)
		})

		sub("socket_togroup_groupuserchange", (e) => {
			store.commit(`friend/setgroupuser`, e)
		})

		sub("socket_touser_newgroup", (e) => {
			store.dispatch({
				type: `friend/addnewgroup`,
				payload: e
			})
		})

		sub("socket_togroup_addgroupuser", (e) => {
			store.commit(`friend/setgroupuser`, e)
		})

		sub("socket_togroup_userexit", (e) => {
			store.commit(`friend/groupuserexit`, e)
		})

		sub("socket_touser_delgroup", (e) => {
			store.commit(`friend/letdelgroup`, e)
		})

		sub("socket_systemupdategroup", (e) => {
			store.commit(`friend/changegrelation`, e)
		})
		
		sub("socket_systemdeletegroup", (e) => {
			store.commit(`friend/deletegroup`, e)
		})
		
		sub("relink", () => {
			store.dispatch({
				type: "friend/init",
				payload: {}
			})
			
			store.dispatch({
				type: "friend/initGroups",
				payload: {}
			})
			
			store.dispatch({
				type: "friend/getAllGroups",
				payload: {}
			})
		})

	}
}